package com.snj.bestdealer.main;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.snj.bestdealer.InjectView;
import com.snj.bestdealer.InjectViewFragment;
import com.snj.bestdealer.R;
import com.snj.bestdealer.lib.CustomHttpClient;
import com.snj.bestdealer.lib.Global;
import com.snj.bestdealer.news.NewsAdapter;
import com.snj.bestdealer.news.NewsContentActivity;
import com.snj.bestdealer.news.NewsItem;

public class HomeFragment extends InjectViewFragment implements OnClickListener {
	private final String TAG = "HomeFragment";
	
	ArrayList<NewsItem> mArrNews = new ArrayList<NewsItem>();
	NewsAdapter mNewsAdapter = null;
	
	@InjectView
	ListView news_list_news;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		View root = inflater.inflate(R.layout.fragment_main_home,
				container, false);
		
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		init_widget();
	}
	
	
	
	private void init_widget() {
		View view = getView();
		
		news_list_news.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				NewsAdapter adapter = (NewsAdapter)news_list_news.getAdapter();
				
				Intent newsintent = new Intent(getActivity(), NewsContentActivity.class);
				newsintent.putExtra("content", adapter.getItem(position).GetContent());
				
				startActivity(newsintent);
				
			}
		});
		
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("appkey", "1234"));

				String result = "";
				JSONObject resultobj = null;
				
				Bitmap img = null;

				try {
					result = CustomHttpClient.executeHttpPost(
							Global.newsurl, postParameters);
					Log.d(TAG, result);
										
					resultobj = new JSONObject(result);

					if(resultobj.getString("result").equals("success"))
					{
						JSONArray arr = resultobj.getJSONArray("newsdata");
						
						for(int i=0; i<arr.length(); i++)
						{
							JSONObject obj = arr.getJSONObject(i);
							img = CustomHttpClient.downloadBitmap(Global.baseurl + "news/" + obj.getString("titleimg"));
							mArrNews.add(new NewsItem("0", obj.getString("title"), obj.getString("content"), "", img, null));
						}
					}		
					else
					{
						return resultobj.getString("fail_cause");
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}

				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				mNewsAdapter = new NewsAdapter(getActivity(), R.layout.list_news, mArrNews);
				news_list_news.setAdapter(mNewsAdapter);
			}
		}.execute("");
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
	}
}
